package com.daily.DailyStudy.Date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 是否在时间差之内
     *
     * @param datetime 传入时间
     * @param min 分钟
     * @return
     * @throws ParseException
     */
    public static Boolean isDiffInMins(String datetime, Integer min) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, min);
        Date after = calendar.getTime();
        calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -min);
        Date before = calendar.getTime();

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = format.parse(datetime);

        if (before.before(date) && after.after(date)) {
            return true;
        }
        return false;
    }

    public static void  main(String [] args) throws ParseException {
        System.out.println(DateUtil.isDiffInMins("20190331164900", 10));
    }

    //当前时间
    public static String nowFormat(String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(new Date());
    }
}
